<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.rapid-framework.org.cn/rapid" prefix="rapid"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<rapid:override name="frame-header-style">
	<style>
/*覆盖 layui*/
.layui-input-block {
	margin: 0px 10px;
}

.layui-table {
	margin-top: 0;
}

.layui-col-md4 {
	padding: 10px;
}

.layui-col-md8 {
	padding: 10px;
}

.layui-btn {
	margin: 2px 0 !important;
}
</style>

</rapid:override>
<rapid:override name="frame-header-script">
	<script src="resources/js/jquery.min.js"></script>
	<script>

	$(function(){
	});
		
</script>
</rapid:override>
<rapid:override name="frame-content">


	<blockquote class="layui-elem-quote">
		<span class="layui-breadcrumb" lay-separator="/"> <a
			href="user/index">首页</a> <a><cite>标签列表</cite></a>
		</span>
	</blockquote>
	<div class="layui-row">
		<div class="layui-col-md4">
			<c:if test="${flag=='edit' }">
				<form class="layui-form" method="post" id="myForm"
					action="tag/saveTag">
					<input type="hidden" value="${tag.tagId }" name="tagId" id="tagId" >
					<div class="layui-form-item">
						<div class="layui-input-block">
							<strong>编辑标签</strong>
						</div>
						<div class="layui-input-block">
							名称 <span style="color: #FF5722;">*</span> <input type="text" id="tagName"
								name="tagName" value="${tag.tagName }" autocomplete="off"
								class="layui-input" required>
						</div>
						<br>
						<div class="layui-input-block">
							标签描述 <input type="text" name="tagDescription" id="tagDescription"
								value="${tag.tagDescription }" autocomplete="off" class="layui-input">
						</div>
						<br>
						<div class="layui-input-block">
							<button class="layui-btn" lay-filter="formDemo" type="button" onclick="return update()">保存</button>
							<a href='tag/list?pageIndex=${pageInfo.pageNum}' class='layui-btn'>去添加标签</a>
						</div>
					</div>
				</form>
			</c:if>
			<c:if test="${flag!='edit' }">
				<form class="layui-form" method="post" id="myForm"
					action="tag/insertTag">
					<div class="layui-form-item">
						<div class="layui-input-block">
							<strong>添加标签</strong>
						</div>
						<div class="layui-input-block">
							名称 <span style="color: #FF5722;">*</span> <input type="text" id="tagName"
								name="tagName" placeholder="请输入标签名称" autocomplete="off"
								class="layui-input" required>
						</div>
						<br>
						<div class="layui-input-block">
							标签描述 <input type="text" name="tagDescription" id="tagDescription"
								placeholder="请输入标签描述" autocomplete="off" class="layui-input">
						</div>
						<br>
						<div class="layui-input-block">
							<button class="layui-btn" lay-filter="formDemo" type="button" onclick="save()">添加</button>
						</div>
					</div>
				</form>
			</c:if>
			
			<blockquote class="layui-elem-quote layui-quote-nm">
				温馨提示：
				<ul>
					<li>1、标签名必选，建议不要太长</li>
					<li>2、标签名勿重复</li>
				</ul>
			</blockquote>
		</div>
		<div class="layui-col-md8">

			<table class="layui-table">
				<colgroup>
					<col width="300">
					<col width="50">
					<col width="100">
					<col width="50">
				</colgroup>
				<thead>
					<tr>
						<th>名称</th>
						<th>文章数</th>
						<th>操作</th>
						<th>ID</th>
					</tr>
				</thead>
				<tbody id="table1">
					<c:forEach var="tag" items="${pageInfo.list }">
						<tr>
							<td><a href="/tag/${tag.tagId }" target="_blank">${tag.tagName }</a></td>
							<td><a href="/tag/${tag.tagId }" target="_blank" lay-data="{sort:true}">${tag.articleCount }</a>
							</td>
							
							<td><a href="tag/edit/${tag.tagId }?pageIndex=${pageInfo.pageNum}"
								class="layui-btn layui-btn-mini" id="${tag.tagId }">编辑</a>
								<c:if test="${tag.articleCount==0 }">
									<a href="tag/delete/${tag.tagId }?pageIndex=${pageInfo.pageNum}"
									class="layui-btn layui-btn-danger layui-btn-mini"
									onclick="return confirmDelete()">删除</a>
								</c:if>	
							</td>
							<td>${tag.tagId }
							</td>
						</tr>
					</c:forEach>
				</tbody>
			</table>
			<nav class="navigation pagination" role="navigation">
				<div class="nav-links">
					<%@ include file="../page.jsp"%>
				</div>
			</nav>
			<blockquote class="layui-elem-quote layui-quote-nm">
				温馨提示：
				<ul>
					<li>如果该标签包含文章，将不可删除</li>
				</ul>
			</blockquote>
		</div>
	</div>

</rapid:override>
<rapid:override name="frame-footer-script">
<script>
/* 添加标签函数 */
function save(){
	var flag = true;
	var tagName = $("#tagName").val();
	if(tagName==""){
		alert("标签名不能为空");
		flag = false;
	}else{
		if(tagName!=""){
			$.ajax({
				url:"tag/tagName",
				type:"post",
				data:{tagName:tagName},
				async:false,
				cache:false,
				success:function(data){
					if(data=="1"){
						alert("用户名重复");
						flag = false;
					}
				}
			});
	}
	}
	if(flag==true){
		$.ajax({
			url:"tag/addTag",
			type:"post",
			data:{tagName:tagName,tagDescription:$("#tagDescription").val()},
			async:false,
			cache:false,
			dataType:"json",
			success:function(tag){
				if(tag!=""||tag!=null){
					alert('添加成功');
					var str="<tr><td><a href='/admin/tag/edit/"+tag.tagId +"' target='_blank'>"+tag.tagName+"</a></td>" 
					+"<td><a href='/admin/tag/edit/"+tag.tagId +"' target='_blank' lay-data='{sort:true}'>"+0+"</a></td>"
					+"<td><a href='/admin/tag/edit/"+tag.tagId +"?pageIndex=${pageInfo.pageNum}' class='layui-btn layui-btn-mini'>编辑</a>"
					+"  <a href='tag/delete/" + tag.tagId +"?pageIndex=${pageInfo.pageNum}' class='layui-btn layui-btn-danger layui-btn-mini' onclick='return confirmDelete()'>删除</a></td>"
					+"<td>" + tag.tagId + "</td></tr>";
				    $("#table1").prepend(str);
				}
				
			}
		});
	}
}
/* 修改标签函数 */
function update(){
	var flag = true;
	var tagName = $("#tagName").val();
	if(tagName!='${tag.tagName}'){
		if(tagName!=""){
			$.ajax({
				url:"tag/tagName",
				type:"post",
				data:{tagName:tagName},
				async:false,
				cache:false,
				success:function(data){
					if(data=="1"){
						alert("用户名重复");
						flag = false;
					}
				}
			});
		}
	}
	if(flag==true){
		$.ajax({
			url:"tag/saveTag",
			type:"post",
			data:{tagName:tagName,tagDescription:$("#tagDescription").val(),tagId:$("#tagId").val()},
			async:false,
			cache:false,
			success:function(date){
				 if(date=="1"){
					alert("修改成功");
				}
				var edit = $("#tagId").val();
				/* $('#'+edit).trigger("click"); 这样不行*/
				document.getElementById(edit).click();
			}
		});
	}
}
/* 删除标签操作 */
function confirmDelete(){
	return confim("确认要删除吗");
}
</script>
</rapid:override>

<%@ include file="../framework.jsp"%>
